package samples.eip;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.context.MessageContextConstants;

public class SimpleBookClient {

    public static void main(String[] args) {
        String repo = "./../synapse_repository";
        testSimpleBooking(repo);
    }

    private static void testSimpleBooking(String repo) {
        try {
            OMFactory factory = OMAbstractFactory.getOMFactory();
        	OMNamespace slbNamespace = factory.createOMNamespace("http://sleepingbeauty.com", "slb");
        	OMElement bookingElem = factory.createOMElement("booking-request", slbNamespace);
        	OMElement nameElem = factory.createOMElement("name", slbNamespace);
        	nameElem.setText("Tijs Rademakers");
		bookingElem.addChild(nameElem);
		OMElement arrivalElem = factory.createOMElement("arrivalDate", slbNamespace);
        	arrivalElem.setText("10-4-2007");
		bookingElem.addChild(arrivalElem);		
		OMElement departureElem = factory.createOMElement("departureDate", slbNamespace);
        	departureElem.setText("20-4-2007");
		bookingElem.addChild(departureElem);

            Options options = new Options();
            options.setProperty(MessageContextConstants.TRANSPORT_URL, "http://localhost:9090");
            options.setAction("http://sleepingbeauty/booking");

            ConfigurationContext configContext =
                ConfigurationContextFactory.createConfigurationContextFromFileSystem(repo, null);
            ServiceClient serviceClient = new ServiceClient(configContext, null);
            serviceClient.setOptions(options);

            //Engage Addressing on outgoing message.
            serviceClient.engageModule(new QName("addressing"));

            OMElement confirmation = serviceClient.sendReceive(bookingElem);
            System.out.println("Confirmation = " + confirmation);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}